﻿//----------------------------------------------------------------
// Copyright (c) 2011-2013 
// All rights reserved.
// Author: Ben DaoWei 
// Contact: bendaowei@gmail.com
// Web Link: http://xnaeditor.codeplex.com
// ----------------------------------------------------------------


using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using System;
using UnityEngine.Graphics;
namespace UnityEngine.Core
{
    public class InputManager
    {
        private UnityEngineGame _Game;
        public InputManager(UnityEngineGame game)
        {
            _Game = game;
        }

        private KeyboardState currentKeyboardState = Keyboard.GetState();
        private MouseState currentMouseState = Mouse.GetState();

        private KeyboardState lastKeyboardState = Keyboard.GetState();
        private MouseState lastMouseState = Mouse.GetState();

        private Rectangle _ClipRectangle;
        public Rectangle ClipRectangle
        {
            get
            {
                return _ClipRectangle;
            }
            set
            {
                _ClipRectangle = value;
            }
        }

        public void Update(GameTime time)
        {
            lastKeyboardState = currentKeyboardState;
            currentKeyboardState = Keyboard.GetState();

            lastMouseState = currentMouseState;
            currentMouseState = Mouse.GetState();

            //Print();
        }

        public MouseState LastMouseState
        {
            get
            {
                return lastMouseState;
            }
        }

        public MouseState CurrentMouseState
        {
            get
            {
                return currentMouseState;
            }
        }

        public int MouseDelta
        {
            get
            {
                return currentMouseState.ScrollWheelValue - lastMouseState.ScrollWheelValue;
            }
        }

        public int MouseX
        {
            get
            {
                return this.currentMouseState.X - this.ClipRectangle.X;
            }
        }

        public int MouseY
        {
            get
            {
                return this.currentMouseState.Y - this.ClipRectangle.Y;
            }
        }

        public int newX(int x)
        {
            return x * _Game.GameSettings.ScreenWidth / SceneManager.DefaultScreenWidth;
        }

        public int newY(int y)
        {
            return y * _Game.GameSettings.ScreenHeight / SceneManager.DefaultScreenHeight;
        }

        public bool MouseInRectangle()
        {
            //System.Diagnostics.Debug.WriteLine(rect.ToString());
            Rectangle rect = this.ClipRectangle;
            if (rect.Contains(this.currentMouseState.X, this.currentMouseState.Y))
                return true;
            else
                return false;
        }

        public bool OnLeftMargin(float margin)
        {
            int offset = this.currentMouseState.X - this.ClipRectangle.Left;
            return (0 <= offset && offset <= margin);
        }

        public bool OnRightMargin(float margin)
        {
            int offset = this.ClipRectangle.Right - this.currentMouseState.X;
            return (0 <= offset && offset <= margin);
        }

        public bool OnTopMargin(float margin)
        {
            int offset = this.currentMouseState.Y - this.ClipRectangle.Top;
            return (0 <= offset && offset <= margin);
        }

        public bool OnBottomMargin(float margin)
        {
            int offset = this.ClipRectangle.Bottom - this.currentMouseState.Y;
            return (0 <= offset && offset <= margin);
        }

        public bool IsKeyJustDown(Keys key)
        {
            return currentKeyboardState.IsKeyDown(key) && lastKeyboardState.IsKeyUp(key);
        }

        public bool IsKeyDown(Keys key)
        {
            return currentKeyboardState.IsKeyDown(key);
        }

        private void Print()
        {
            System.Diagnostics.Debug.WriteLine(Mouse.GetState().ToString());
        }
    }
}
